﻿using System;
using System.Collections.Generic;
using System.Data;
//using System.Data.SqlServerCe;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace AMS.Inventory.PCClient.InventoryHelpClass
{
    class SqlSdfDb
    {
        private string _DataSource = Application.StartupPath + @"\PDA_AMS_Inventory.db3";
       
        public string DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                var str = value;
                str = str.Trim();
                int len = str.Length;
                str = str.Substring(len - 3, 3);

                if (str != "sdf")
                {
                    MessageBox.Show("File type is not right!", "Warning");
                }
                else
                {
                    this._DataSource = value;
                    this.ConnectDbClose();
                    this.ConnectDbOpen();
                }
            }
        }
        public bool ShowExceptionMessage = false;
        //public SQLiteCommand ResultSetCommand = null;
        //public SQLiteResultSet GetReaderResult(string sql)
        //{
        //    SQLiteResultSet OrderListResultSet = null;
        //    try
        //    {
        //        ResultSetCommand = DbConnection.CreateCommand();
        //        ResultSetCommand.CommandText = sql;
        //        OrderListResultSet = ResultSetCommand.ExecuteResultSet(ResultSetOptions.Scrollable);
        //    }
        //    catch (Exception ex)
        //    {
        //        if (this.ShowExceptionMessage)
        //            MessageBox.Show(ex.Message);
        //    }
        //    return OrderListResultSet;
        //}

        private SQLiteConnection DbConnection = null;
        public SQLiteConnection SQLiteDbConnection
        {
            get
            {
                return DbConnection;
            }
        }

        public void ConnectDbClose()
        {
            if (DbConnection != null)
            {
                try
                {
                    DbConnection.Close();
                    DbConnection.Dispose();
                    DbConnection = null;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

        }
        //用数据连接对象打开 SDF 文件
        public bool OpenConnection()
        {
            try
            {
                DbConnection = null;

                DbConnection = new SQLiteConnection("Data Source = " + this._DataSource + ";");
                DbConnection.Open();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public void ConnectDbOpen()
        {
            if (File.Exists(this._DataSource))
            {
                try
                {
                    DbConnection = new SQLiteConnection("Data Source = " + this._DataSource + "; ");
                    DbConnection.Open();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show(this._DataSource + " not Exist!", "Warning");
            }
        }

        public int ExecNoneQuery(string sql)
        {
            int result = -1;
            SQLiteConnection celCon = null;
            SQLiteCommand cmd = null;
            try
            {

                celCon = new SQLiteConnection("Data Source = " + this._DataSource + "; ");

                cmd = new SQLiteCommand(sql, celCon);//celCon.CreateCommand();
                celCon.Open();

                result = cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {



            }
            finally
            {
                celCon.Close();
                cmd.Dispose();
                cmd = null;
            }

            return result;
        }
       
        public int ExecNoneQueryByList(StringBuilder  sb )
        {
            int result = -1;
            var celCon = new SQLiteConnection("Data Source = " + this._DataSource + "; ");
            var cmd = new SQLiteCommand(celCon);//celCon.CreateCommand();
            celCon.Open();
            try{
                   cmd.CommandText = sb.ToString();
                   result = cmd.ExecuteNonQuery();
               }
            catch (Exception ex)
            {

                throw ex;

            }
            finally
            {
                celCon.Close();
                cmd.Dispose();
                
            }
            return result;
        }

        public int ExecNoneQueryByList(StringBuilder sb,SQLiteCommand comm)
        {
            int result = -1;


            comm.CommandText = sb.ToString();
            result = comm.ExecuteNonQuery();

            return result;
        }

        public DataTable GetDataTable(string sql)
        {


            var table = new DataTable();

            var celCon = new SQLiteConnection("Data Source = " + _DataSource + "; ");

            celCon.Open();
            var adp = new SQLiteDataAdapter(sql, celCon);

            try
            {
                adp.Fill(table);



            }
            catch (Exception ex)
            {
                if (this.ShowExceptionMessage)
                    MessageBox.Show(ex.StackTrace);
                return null;
            }
            finally
            {
                celCon.Close();
                adp.Dispose();
            }
            return table;
        }
    }
}